package userdao

import (
	"power/core/dao/table"
	"power/core/localwrap"
	"power/core/service/common"

	"github.com/RocksonZeta/wrap/redised"
	"github.com/RocksonZeta/wrap/utils/sutil"
	"github.com/RocksonZeta/wrap/utils/timeutil"
)

const tableUserPrivate = "UserPrivate"
const tableUserPrivateId = "Id"

var logUserPrivates = localwrap.GetLogger().Fork("power/core/dao/userdao", "UserPrivates")

//UserPrivates
type UserPrivates struct {
	*redised.RedisedMysql
}

type UserPrivatesQueryParam struct {
	common.Page
	Name string
}

//Query 根据条件查询
func (s UserPrivates) Query(param UserPrivatesQueryParam) ([]table.UserPrivate, int64) {
	logUserPrivates.Debug().Func("Query").Interface("param", param).Send()
	wsql := " from UserPrivate where 1=1"
	if param.Name != "" {
		param.Name = "%" + param.Name + "%"
		wsql += " and name like :Name"
	}
	ssql := "select *" + wsql + " order by ct desc" + param.Limit()
	csql := "select count(*)" + wsql
	var r []table.UserPrivate
	s.Mysql.Select(&r, ssql, param)
	return r, s.Mysql.SelectInt(csql, param)
}

func (s UserPrivates) Get(id int) table.UserPrivate {
	logUserPrivates.Debug().Func("Get").Int("id", id).Send()
	var r table.UserPrivate
	s.RedisedMysql.Get(&r, tableUserPrivate, tableUserPrivateId, id)
	return r
}

func (s UserPrivates) List(ids []int) []table.UserPrivate {
	logUserPrivates.Debug().Func("List").Ints("ids", ids).Send()
	var r []table.UserPrivate
	s.RedisedMysql.List(&r, tableUserPrivate, tableUserPrivateId, ids)
	return r
}
func (s UserPrivates) ListAsMap(ids []int) map[int]table.UserPrivate {
	logUserPrivates.Debug().Func("ListAsMap").Ints("ids", ids).Send()
	l := s.List(ids)
	r := make(map[int]table.UserPrivate, len(l))
	for _, v := range l {
		r[v.Id] = v
	}
	return r
}

func (s UserPrivates) Delete(id int) {
	logUserPrivates.Debug().Func("Delete").Int("id", id).Send()
	old := s.Get(id)
	s.Mysql.Delete(tableUserPrivate, tableUserPrivateId, id)
	s.ClearCache(old)
}

func (s UserPrivates) ClearCache(old table.UserPrivate) {
	logUserPrivates.Debug().Func("ClearCache").Int("id", old.Id).Send()
	s.Redis.Del(s.KeyFn(tableUserPrivate, old.Id))
	s.Redis.Del(s.KVFn(tableUserPrivate, map[string]interface{}{"uid": old.Uid}))
}
func (s UserPrivates) GetByUid(uid int) table.UserPrivate {
	logUserPrivates.Debug().Func("GetByUid").Int("uid", uid).Send()
	var r table.UserPrivate
	s.RedisedMysql.GetByKvs(&r, tableUserPrivate, tableUserPrivateId, map[string]interface{}{"uid": uid})
	return r
}
func (s UserPrivates) GetByEmail(email string) table.UserPrivate {
	logUserPrivates.Debug().Func("GetByEmail").Str("email", email).Send()
	var r table.UserPrivate
	s.RedisedMysql.Mysql.GetBy(&r, tableUserPrivate, map[string]interface{}{"email": email})
	return r
}

type UserPrivatesUpdateParam struct {
	Id       int
	Password string
	Email    string
	Mobile   string
}

func (s UserPrivates) Update(param UserPrivatesUpdateParam) {
	logUserPrivates.Debug().Func("Update").Interface("param", param).Send()
	s.Mysql.Patch(tableUserPrivate, tableUserPrivateId, param)
	s.Redis.Del(s.KeyFn(tableUserPrivate, param.Id))
	// s.ClearCache(s.Get(param.Id)) if no index updated
}

type UserPrivatesAddParam struct {
	Uid      int
	Password string
	Email    string
	Mobile   string
}

func (s UserPrivates) Add(param UserPrivatesAddParam) table.UserPrivate {
	logUserPrivates.Debug().Func("Add").Interface("param", param).Send()
	var t table.UserPrivate
	sutil.Copy(param, &t)
	t.Ct = timeutil.Now()
	s.Mysql.AddTable(t)
	s.Mysql.Insert(&t)
	s.ClearCache(t)
	return t
}
